package viqiang.ye.mallpuny.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import viqiang.ye.mallpuny.common.api.CommonPage;
import viqiang.ye.mallpuny.common.api.CommonResult;
import viqiang.ye.mallpuny.mbg.model.PmsBrand;
import viqiang.ye.mallpuny.service.PmsBrandService;

import java.util.List;

@Api(tags = "PmsBrandController", description = "商品品牌管理")
@Controller
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    @ApiOperation("添加品牌")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createBrand(@RequestBody PmsBrand pmsBrand) {
        CommonResult result;
        int count = pmsBrandService.create(pmsBrand);
        if (count == 1) {
           result = CommonResult.success(pmsBrand);
        } else {
            result = CommonResult.failed("添加品牌事变");
        }
        return result;
    }

    @ApiOperation("品牌分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsBrand>> getBrandList(@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                                       @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize) {
        List<PmsBrand> pmsBrandList = pmsBrandService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(pmsBrandList));
    }

    @ApiOperation("更新品牌")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateBrand(@PathVariable("id")Long id, @RequestBody PmsBrand pmsBrand, BindingResult bindingResult) {
        CommonResult result;
        int count = pmsBrandService.update(id, pmsBrand);
        if (count ==1) {
            result = CommonResult.success(pmsBrand);
        }else {
            result = CommonResult.failed("品牌更新失败！");
        }
        return result;
    }

    @ApiOperation("删除品牌")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteBrand(@PathVariable("id")Long id) {
        int count = pmsBrandService.delete(id);
        if (count == 1) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("查询单个品牌")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> detail(@PathVariable("id") Long id) {
        return CommonResult.success(pmsBrandService.detail(id));
    }

}
